package com.bsc.rtc.lib.libraries.wedgit;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.OverScroller;

public class OverScrollLayout extends ViewGroup {

    private OverScroller mScroller;

    public OverScrollLayout(Context context) {
        super(context);
        init(context);
    }

    public OverScrollLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public OverScrollLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mScroller = new OverScroller(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                currentX = (int) ev.getRawX();
                currentY = (int) ev.getRawY();
                return true;
                //break;

        }
        return super.onInterceptTouchEvent(ev);
    }
    int leftBorder;//左边界
    int rightBorder;//右边界
    int x;//手指落下的横坐标
    int y;
    int currentX;//手指滑动的横坐标
    int currentY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
              /*  mScroller.startScroll(0, 0, (x - currentX), 0);
                invalidate();*/
                x = (int) event.getRawX();
                scrollTo((currentX-x), 0);
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            // 为ScrollerLayout中的每一个子控件测量大小
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (changed) {
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                View childView = getChildAt(i);
                // 为ScrollerLayout中的每一个子控件在水平方向上进行布局
                // childView.layout(i * childView.getMeasuredWidth(), 0, (i + 1) * childView.getMeasuredWidth(), childView.getMeasuredHeight());
                layoutItem(i);
            }
            leftBorder=getChildAt(0).getLeft();
            rightBorder=getChildAt(getChildCount()-1).getRight();
        }
    }

    //放置每一个item位置
    private void layoutItem(int index) {
        int left = 0;
        int top = 0;
        int right = 0;
        int buttom = 0;
        for (int i = 0; i <= index - 1; i++) {
            //Log.i("zzw", "current=" + i + " width=" + getChildAt(i).getMeasuredWidth());
            left += getChildAt(i).getMeasuredWidth();
        }
        right = left + getChildAt(index).getMeasuredWidth();
        buttom = getChildAt(index).getMeasuredHeight();
        View currentView = getChildAt(index);
        //Log.i("zzw", "left==" + left + " right" + right);
        currentView.layout(left, top, right, buttom);
    }

    @Override
    public void computeScroll() {

        //先判断mScroller滚动是否完成
        if (mScroller.computeScrollOffset()) {

            //这里调用View的scrollTo()完成实际的滚动
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());

            //必须调用该方法，否则不一定能看到滚动效果
            postInvalidate();
        }
        super.computeScroll();
    }
}
